<?php

namespace app\helps;
use think\Db;

class SmsHelper{

//    function http_request($url,$data = null,$headers=array()){
//        $curl = curl_init();
//        if( count($headers) >= 1 ){
//            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//        }
//        curl_setopt($curl, CURLOPT_URL, $url);
//
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//
//        if (!empty($data)){
//            curl_setopt($curl, CURLOPT_POST, 1);
//            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//        }
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//        $output = curl_exec($curl);
//        curl_close($curl);
//        return json_decode($output,true);
//    }

    /**
     * 模拟post进行url请求
     * @param string $url
     * @param array $post_data
     */
    function request_post($url = '', $post_data = array()) {
        if(!is_array($post_data)){
            throw new Exception("参数必须为array");

        }
        $headers  = array(
            'Accept:'.'application/json;charset=utf-8',
            'Content-Type:'.'application/x-www-form-urlencoded;charset=utf-8'
        );
        $param = http_build_query($post_data);
        $httph =curl_init($url);
        curl_setopt($httph, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);

        curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 2);

        curl_setopt($httph,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($httph,CURLOPT_HEADER,0);
        curl_setopt($httph, CURLOPT_POST, 0);//设置为POST方式

        curl_setopt($httph, CURLOPT_POSTFIELDS, $param);

        curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);

        $rst=curl_exec($httph);
        curl_close($httph);
        return json_decode($rst);


    }

    /**
     * 发送短信验证码
     * @param $phone
     * @param $code
     * @return mixed
     */
    public function sendSmsCode($phone,$code){
        $smsapi = 'https://sms.yunpian.com/v2/sms/single_send.json';
        $content ="【MT】您的短信验证码为".$code."，3分钟内有效，请勿向任何人提供此验证码。";
        $postData = array();
        $postData["apikey"] = "d398a87aacb629cf2ed75a9ddd17d6a2";
        $postData["mobile"] =$phone;
            $postData["text"] =$content;
        return $this->request_post($smsapi,$postData);
    }

    /**
     * 订单匹配发送短信验证码
     * @param $phone
     * @param $code
     * @return mixed
     */
    public function sendSmsByMacherOrder($phone,$content){
        $smsapi = 'https://sms.yunpian.com/v2/sms/single_send.json';
        $postData = array();
        $postData["apikey"] = "d398a87aacb629cf2ed75a9ddd17d6a2";
        $postData["mobile"] =$phone;
        $postData["text"] =$content;
        return $this->request_post($smsapi,$postData);
    }

    /**
     *
     * @param $phone 发送多个手机号请以英文逗号分隔，建议单次提交 200 个手机号以内，不要超过 1000 个，不需要带+86 前缀
     * 订单匹配发送短信验证码（批量发送）
     * @param $content
     */
    public function sendBatchSmsByMacherOrder($phone,$content){
        $smsapi = 'https://sms.yunpian.com/v2/sms/batch_send.json';
        $postData = array();
        $postData["apikey"] = "d398a87aacb629cf2ed75a9ddd17d6a2";
        $postData["mobile"] =$phone;
        $postData["text"] =$content;
        return $this->request_post($smsapi,$postData);
    }

    /**
     * 订单匹配发送短信验入库 等定时任务 轮训发送
     * @param $phone
     * @param $code
     * @return mixed
     */
    public function sendSmsByMacherOrderRecord($uid,$phone,$osn){
        $data=[];
        $data["mobile"]= $phone;
        $data["uid"]= $uid;
        $data["time"]= date('Y-m-d H:i:s');
        $data["num"]= 0;
        $data["osn"]= $osn;
        $data["suc"]= 0;
        $data["status"]= 1;
        $data["content"]= "【MT】尊敬的用户，恭喜您成功预购获奖！请尽快登录系统进行处理，在20:00之前不处理将会锁定账号哦！";
        $res = Db::table("tb_mt_order_sms")->insert($data);
        if (!$res){
            return "发送短信记录入库失败";
        }
        return 1;
    }

    /**
     * 订单匹配发送短信验证码
     * @param $phone
     * @param $code
     * @return mixed
     */
    public function sendSmsResult($record,$suc){
        $data=[];
        $data["time"]= date('Y-m-d H:i:s');
        $data["num"]= $record["num"]+1;
        $data["suc"]= $suc;
        $data["status"]= 1;
        $res = Db::table("tb_mt_order_sms")->where("id",$record["id"])->update($data);
        if (!$res){
            return "发送短信记录入库失败";
        }
        return 1;
    }

}